package com.ruoyi.qcsystem.mapper;

import com.ruoyi.qcsystem.domain.entity.QualityConfirmation;
import com.ruoyi.qcsystem.domain.entity.QualityMoldConfirmation;
import com.ruoyi.qcsystem.service.QcMoldDataService;
import com.ruoyi.ruoyictmanage.domain.entity.Line;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface QcMoldDataMapper {

    @Results(id = "userEmilMap", value = {
            @Result(property = "qcId", column = "qc_id"),
            @Result(property = "lineId", column = "line_id"),
            @Result(property = "machineNumber", column = "machine_number"),
            @Result(property = "qcfF", column = "qcf_F"),
            @Result(property = "qcfM", column = "qcf_M"),
            @Result(property = "qcfE", column = "qcf_E"),
            @Result(property = "qceF", column = "qce_F"),
            @Result(property = "qceM", column = "qce_M"),
            @Result(property = "qceE", column = "qce_E"),
            @Result(property = "moldCheck", column = "mold_check"),
            @Result(property = "userId", column = "user_id")
    })
    @Select("<script>" +
            "select * from quality_mold_confirmation where  1=1 " +
            "<if test='lineId != null and lineId !=\"\" '> and line_id=#{lineId}</if>" +
            "<if test='starTime != null and starTime !=\"\" and endTime != null and endTime !=\"\" '>" +
            " and date BETWEEN #{starTime} and #{endTime}</if>" +
            "<if test='mpn != null and mpn !=\"\" '> and " +
            " mpn like CONCAT('%', #{mpn}, '%')</if>" +
            "<if test='lot != null and lot !=\"\" '> and " +
            " lot like CONCAT('%', #{lot}, '%')</if>" +
            "</script>")
    List<QualityMoldConfirmation> qualityConfirmationList(@Param("lineId") int lineId, @Param("starTime") String starTime, @Param("endTime") String endTime, @Param("mpn") String mpn, @Param("lot") String lot);

    @Select("select * from line")
    List<Line> lineList();

    @Options(useGeneratedKeys = true, keyProperty = "qualityConfirmation.qcId", keyColumn = "qc_id")
    @Insert("INSERT INTO quality_mold_confirmation (\n" +
            "        line_id,\n" +
            "        date,\n" +
            "        shift,\n" +
            "        mpn,\n" +
            "        lot,\n" +
            "        machine_number,\n" +
            "        qcf_f,\n" +
            "        qcf_m,\n" +
            "        qcf_e,\n" +
            "        qce_f,\n" +
            "        qce_m,\n" +
            "        qce_e,\n" +
            "        admit,\n" +
            "        mold_check,"+
            "        confirmer,\n" +
            "        user_id,\n" +
            "        remark\n" +
            "    ) VALUES (\n" +
            "        #{qualityConfirmation.lineId},\n" +
            "        now(),\n" +
            "        #{qualityConfirmation.shift},\n" +
            "        #{qualityConfirmation.mpn},\n" +
            "        #{qualityConfirmation.lot},\n" +
            "        #{qualityConfirmation.machineNumber},\n" +
            "        #{qualityConfirmation.qcfF},\n" +
            "        #{qualityConfirmation.qcfM},\n" +
            "        #{qualityConfirmation.qcfE},\n" +
            "        #{qualityConfirmation.qceF},\n" +
            "        #{qualityConfirmation.qceM},\n" +
            "        #{qualityConfirmation.qceE},\n" +
            "        #{qualityConfirmation.admit},\n" +
            "        #{qualityConfirmation.moldCheck},\n" +
            "        #{qualityConfirmation.confirmer},\n" +
            "        #{qualityConfirmation.userId},\n" +
            "        #{qualityConfirmation.remark}\n" +
            "    )")
    int addQualityConfirmation(@Param("qualityConfirmation") QualityMoldConfirmation qualityConfirmation);

    @Select("select machine_quantity from machine_number where line_id=#{lineId} and process_id=#{processId}")
    int getMachineNumber(@Param("lineId") Integer lineId,@Param("processId") long processId);

    @Update("UPDATE quality_mold_confirmation SET\n" +
            "    line_id = #{qualityConfirmation.lineId},\n" +
            "    shift = #{qualityConfirmation.shift},\n" +
            "    mpn = #{qualityConfirmation.mpn},\n" +
            "    lot = #{qualityConfirmation.lot},\n" +
            "    machine_number = #{qualityConfirmation.machineNumber},\n" +
            "    qcf_f = #{qualityConfirmation.qcfF},\n" +
            "    qcf_m = #{qualityConfirmation.qcfM},\n" +
            "    qcf_e = #{qualityConfirmation.qcfE},\n" +
            "    qce_f = #{qualityConfirmation.qceF},\n" +
            "    qce_m = #{qualityConfirmation.qceM},\n" +
            "    qce_e = #{qualityConfirmation.qceE},\n" +
            "    mold_check = #{qualityConfirmation.moldCheck},\n" +
            "    remark = #{qualityConfirmation.remark}\n" +
            "WHERE qc_id = #{qualityConfirmation.qcId}")
    int updateQualityConfirmation(@Param("qualityConfirmation") QualityMoldConfirmation qualityConfirmation);

    @Update("UPDATE quality_confirmation SET confirmer=#{confirmer} where qc_id=#{qcId}")
    int confirmQualityConfirmation(@Param("qcId") Long qcId,@Param("confirmer") String confirmer);

    @Select("select count(*) from quality_mold_confirmation where user_id=#{userId} and qc_id=#{qcId} ")
    int getQualityConfirmationCountByUserId(@Param("userId") Long userId,@Param("qcId") Long qcId);

    @Delete("delete from quality_mold_confirmation where qc_id=#{qcId}")
    int deleteQualityConfirmation(@Param("qcId") Long qcId);

    @Select("SELECT COALESCE((\n" +
            "    SELECT \n" +
            "        CASE WHEN COALESCE(qcf_f, '') = '' OR COALESCE(qcf_m, '') = '' OR \n" +
            "                  COALESCE(qcf_e, '') = '' OR COALESCE(qce_f, '') = '' OR \n" +
            "                  COALESCE(qce_m, '') = '' OR COALESCE(qce_e, '') = '' \n" +
            "             THEN 0 ELSE 1 \n" +
            "        END AS result\n" +
            "    FROM quality_mold_confirmation \n" +
            "    WHERE line_id = #{lineId} \n" +
            "    ORDER BY date desc\n" +
            "    LIMIT 1\n" +
            "), 0) AS result;")
    int getNewMoldDataIfNull(@Param("lineId")int lineId);
}
